const observerCaller = (function(){
    const message = {};
    return {
        regisit: (key, caller) => { // 注册
            if (typeof message[key] === 'undefined') {
                message[key] = [caller];
            } else {
                message[key].push(caller);
            }
        },
        fire: (key, args) => { // 发布
            if (typeof message[key] === 'undefined') {
                return;
            }
            const events = args;
            let i = 0;
            for(; i < message[key].length; i++) {
                message[key][i].call(this, events);
            }
        },
        remove: (key) => { // 移除
            setTimeout(() => {
                // 检测原型链上是否存在Array
                let i = message[key].length -1;
                if (message[key] instanceof Array) {
                    for (; i >= 0; i--) {
                        message[key].splice(i, 1);
                    }
                }
                if (message[key].length === 0) {
                    delete message[key];
                }
            }, 0);
        }
    }
})();
export default observerCaller;
